package com.hm.shopping.task;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.DateUtil;
import com.hm.core.SdkConfig;
import com.hm.shopping.auto.SystemEnvConfigProperties;
import com.hm.shopping.def.OrderStateEnum;
import com.hm.shopping.service.OrderService;
import com.hm.shopping.vo.su.BuyGoodsSnapshotVo.GoodsIntroductionSnapshotVo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2018年2月27日 上午10:39:32
 * @version 1.0
 * @describe 订单自动结算
 */
@Slf4j
@Component
public class FinishOrderTask {

	@Autowired
	private OrderService orderService;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;

	@Scheduled(cron = "${com.hm.shopping.init.finishOrder.cron}")
	public void doTask() {
		log.info(DateUtil.now4yyyyMMddhhmm() + "执行系统订单自动收货");

		int pageNumber = 1;
		do {
			PageInfo<GoodsIntroductionSnapshotVo> page = orderService.queryBuyerGoodsSnapshots(new PageInfoParam(pageNumber, 100), SdkConfig.SUPER_ADMIN + "", OrderStateEnum.FINISH_COURIER);
			if (CommonUtil.isEmpty(page.getContent())) {
				return;
			}
			page.getContent().parallelStream().filter(item -> (DateUtil.plusDay(new Date(item.getUpdateTime()), systemEnvConfigProperties.getFinishOrder().getInterval()).getTime() < System.currentTimeMillis())).forEach(item -> orderService.finishOrder(item.getId()));
			pageNumber++;
		} while (true);
	}
}
